package com.yygo.jobs;

import com.yygo.Constants;
import com.yygo.model.Activity;
import com.yygo.model.GrabResponse;
import com.yygo.model.LoanRequest;
import com.yygo.model.User;
import com.yygo.model.enums.Phase;
import com.yygo.service.LoanRequestManager;
import com.yygo.service.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.messaging.simp.SimpMessagingTemplate;

import java.util.Locale;

/**
 * Created by tiansha on 2015/10/29.
 */
public class GrabTicketJob {

    @Autowired
    private UserManager userManager;

    @Autowired
    private LoanRequestManager loanRequestManager;

    private MessageSource messageSource;
    private MessageSourceAccessor messages;

    @Autowired
    public void setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
        messages = new MessageSourceAccessor(messageSource);
    }

    public boolean grabTicket(LoanRequest loanRequest, String requestor) {
        if (loanRequest.getAuditUser() != null) {
            return false;
        }
        User requestUser = userManager.getUserByUsername(requestor);
        // check if the user can audit the request
        /*if ((loanRequest.getPhase().equals(Phase.cc_audit) && requestUser.hasRole(Constants.CC_ROLE))
                || (loanRequest.getPhase().equals(Phase.ca_audit) && requestUser.hasRole(Constants.CA_ROLE))
                || (loanRequest.getPhase().equals(Phase.sca_audit) && requestUser.hasRole(Constants.SCA_ROLE))
                || (loanRequest.getPhase().equals(Phase.bank_credit_audit)
                && requestUser.hasRole(Constants.Bank_CREDIT_ROLE) && requestUser.getBank().equals(loanRequest.getLoanProduct().getBank()))
                || (loanRequest.getPhase().equals(Phase.bank_loan_audit)
                && requestUser.hasRole(Constants.Bank_USER_ROLE) && requestUser.getBank().equals(loanRequest.getLoanProduct().getBank()))) {*/
            loanRequest.setAuditUser(requestUser);
            //String readableStatus = getText("PHASE_".concat(loanRequest.getPhase().toString()), Locale.SIMPLIFIED_CHINESE);
            String activity = getText("loanRequest.activity.transferOperator", requestor, Locale.SIMPLIFIED_CHINESE);
            Activity activityObj = new Activity(requestUser.getUsername(), activity);
            activityObj.setRole(requestUser.getRoleList().get(0).getValue());
            activityObj.setLoanRequest(loanRequest);
            loanRequest.getActivities().add(activityObj);
            loanRequestManager.saveLoanRequest(loanRequest);
            return true;
        //}
        //return false;
    }

    private String getText(String msgKey, Locale locale) {
        return messages.getMessage(msgKey, locale);
    }

    private String getText(String msgKey, String arg, Locale locale) {
        return getText(msgKey, new Object[]{arg}, locale);
    }

    private String getText(String msgKey, Object[] args, Locale locale) {
        return messages.getMessage(msgKey, args, locale);
    }


}
